package com.dd.ent.car.ui.home.templet.Base;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.dd.ent.car.R;
import com.dd.ent.car.common.bean.base.PageBean;
import com.dd.ent.car.common.bean.base.ResultBean;
import com.dd.ent.car.ui.adapter.base.BaseListAdapter;
import com.dd.ent.car.ui.empty.EmptyLayout;
import com.dd.ent.car.ui.widget.RefreshPromptView;
import com.dd.ent.car.ui.widget.SuperRefreshLayout;
import com.pp.ent.view.annotation.ContentView;
import com.pp.ent.view.annotation.ViewInject;

import java.lang.reflect.Type;
import java.util.Date;

/**
 * T as the base bean
 * Created by dengqu
 * on 16-5-23.
 */
@ContentView(R.layout.fragment_base_list)
public abstract class TempletBaseListFragment<T> extends TempletBaseFragment implements
        SuperRefreshLayout.SuperRefreshLayoutListener,
        AdapterView.OnItemClickListener, BaseListAdapter.Callback,
        View.OnClickListener {
    private final static String TAG = "HomeBaseListFragment";
    public static final int TYPE_NORMAL = 0;
    public static final int TYPE_LOADING = 1;
    public static final int TYPE_NO_MORE = 2;
    public static final int TYPE_ERROR = 3;
    public static final int TYPE_NET_ERROR = 4;
    protected int ONE_PAGE_MAX_SIZE = 10;

    @ViewInject(R.id.listView)
    protected ListView mListView;
    @ViewInject(R.id.superRefreshLayout)
    protected SuperRefreshLayout mRefreshLayout;
    @ViewInject(R.id.error_layout)
    protected EmptyLayout mErrorLayout;

    @ViewInject(R.id.tex_refreshPro)
    private RefreshPromptView refreshPromptView;

    protected BaseListAdapter<T> mAdapter;
    protected boolean mIsRefresh;
    protected PageBean<T> mBean;
    private View mFooterView;
    private ProgressBar mFooterProgressBar;
    private TextView mFooterText;

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        mRefreshLayout.setColorSchemeResources(
                R.color.swiperefresh_color1, R.color.swiperefresh_color2,
                R.color.swiperefresh_color3, R.color.swiperefresh_color4);
        mRefreshLayout.setSuperRefreshLayoutListener(this);
        mRefreshLayout.setPlayTag(getPlayTag());
        mFooterView = LayoutInflater.from(getContext()).inflate(R.layout.layout_list_view_footer, null);
        mFooterText = (TextView) mFooterView.findViewById(R.id.tv_footer);
        mFooterProgressBar = (ProgressBar) mFooterView.findViewById(R.id.pb_footer);
        //mListView.setOnItemClickListener(this);
        setFooterType(TYPE_LOADING);
        mErrorLayout.setOnLayoutClickListener(this);
        if (isNeedFooter())
            mListView.addFooterView(mFooterView);

    }

    protected String getPlayTag() {
        return null;
    }

    @Override
    protected void initData() {
        super.initData();
        mAdapter = getListAdapter();
        mListView.setAdapter(mAdapter);

    }

    @Override
    public void onClick(View v) {
        mErrorLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        onRefreshing();
    }

    @Override
    public void onRefreshing() {
        mIsRefresh = true;
        requestData();
    }

    @Override
    public void onLoadMore() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                requestData();
            }
        }, 2000);
    }

    /**
     * request network data
     */
    protected void requestData() {
        onRequestStart();
        setFooterType(TYPE_LOADING);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    protected void onRequestStart() {

    }

    protected void onRequestError(int code) {
        setFooterType(TYPE_NET_ERROR);
        if (mAdapter.getDatas().size() == 0)
            mErrorLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
    }

    protected void onRequestFinish() {
        onComplete();
    }

    protected void onComplete() {
        mRefreshLayout.onLoadComplete();
        mIsRefresh = false;
    }

    protected void setListData(ResultBean<PageBean<T>> resultBean) {
        if (mIsRefresh) {
            showRefreshPromptView(resultBean);
            mBean.setItems(resultBean.getResult().getItems());
            mAdapter.clear();
            mAdapter.addItem(mBean.getItems());
            mBean.setPrevPageToken(resultBean.getResult().getPrevPageToken());
        } else {
            mBean = resultBean.getResult();
            mAdapter.addItem(resultBean.getResult().getItems());
        }
        if (resultBean.getResult().getItems().size() < ONE_PAGE_MAX_SIZE) {
            setFooterType(TYPE_NO_MORE);
            mRefreshLayout.setNoMoreData();
        } else {
            mRefreshLayout.setCanLoadMore();
        }
        if (mAdapter.getDatas().size() > 0) {
            mErrorLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
            mRefreshLayout.setVisibility(View.VISIBLE);
        } else {
            mErrorLayout.setErrorType(EmptyLayout.NODATA);
        }
    }

    protected void showRefreshPromptView(ResultBean<PageBean<T>> resultBean) {
        refreshPromptView.show(resultBean.getResult().getItems().size());
    }

    @Override
    public Date getSystemTime() {
        return new Date();
    }

    protected abstract BaseListAdapter<T> getListAdapter();

    protected abstract Type getType();

    protected boolean isNeedFooter() {
        return true;
    }

    protected void setFooterType(int type) {
        switch (type) {
            case TYPE_NORMAL:
            case TYPE_LOADING:
                mFooterText.setText(getResources().getString(R.string.footer_type_loading));
                mFooterProgressBar.setVisibility(View.VISIBLE);
                break;
            case TYPE_NET_ERROR:
                mFooterText.setText(getResources().getString(R.string.footer_type_net_error));
                mFooterProgressBar.setVisibility(View.GONE);
                break;
            case TYPE_ERROR:
                mFooterText.setText(getResources().getString(R.string.footer_type_error));
                mFooterProgressBar.setVisibility(View.GONE);
                break;
            case TYPE_NO_MORE:
                mFooterText.setText(getResources().getString(R.string.footer_type_not_more));
                mFooterProgressBar.setVisibility(View.GONE);
                break;
        }
    }
}
